package com.zgc.view.validator;

import com.jfoenix.controls.base.IFXValidatableControl;
import com.jfoenix.validation.base.ValidatorBase;

import java.util.function.Function;

/**
 * 自定义校验
 *
 * @param <T>
 */
public class FieldValidator<T> extends ValidatorBase {
    private ValidatorRunning<T> running;

    public FieldValidator() {
    }

    public FieldValidator(String message) {
        super(message);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void eval() {
        if ((getSrcControl() instanceof IFXValidatableControl)) {
            ((IFXValidatableControl) getSrcControl()).resetValidation();
            hasErrors.set(running.run((T) getSrcControl()));
        }
    }

    /**
     * 自定义 校验事件
     *
     * @param running 校验的具体逻辑
     */
    public void validatorRunning(ValidatorRunning<T> running) {
        this.running = running;
    }

}
